<?php
namespace app\common\model;

use Think\Db;
use think\Model;

class TaskUser extends Model
{
    // 指定表名,不含前缀
    protected $name = 'task_user';
    protected $tableName = 'ex_task_user';

    /* 获取USER表中人所有用户到任务列表 */

    public function getAllUserToTaskUser($taskid,$task_type, $userid = 0)
    {
        if (empty($taskid))
            return false;
        try {
            $time = time();
            $sql = "replace into ".$this->tableName." (identity_id,task_id,task_completeness,is_complete,task_type,create_time) ";
            $sql .= "select uc.id,{$taskid},0,0,{$task_type},{$time} ";
            $sql .= "from ex_user_company as uc left join ex_user as u on uc.userid=u.userid "; //排除超级管理员
            $sql .= "where uc.admin_reg='{$_SESSION['think']['reg']}' and uc.userid>0 and uc.status=0 and uc.isdelete=0 ";
            if (!empty($userid)) {
                if (is_array($userid))
                    $userid = implode(',', $userid);
                $sql .= " and uc.userid in ('{$userid}') ";
            }
            $sql .= " and uc.id not in (select identity_id from ".$this->tableName." where task_id=" . $taskid;

            if (!empty($userid))
                $sql .= " and userid in('{$userid}')";
            $sql .= ")";
            $this->execute($sql);
        } catch (Exception $ex) {
            \SLogger::getLogger();
            return FALSE;
        }
        return TRUE;
    }

    public function deleteTaskUserByTaskId($taskid){
        if (empty($taskid))
            return false;
       $res =  Db::name('task_user')->where(['task_id'=>['in',$taskid]])->delete();
        if ($res){
            return TRUE;
        }
        return false;
    }
}
